/**
 * Created by jingfang on 16/11/23.
 */
this.AnimatedObject = this.AnimatedObject || {};
/**
 * 动态和曲线
 */
(function(){
    var animatedCurve = function(graphicsName)
    {
        this._lineGraphics = new PIXI.Graphics();
        this._lineGraphics._graphicsName = graphicsName;
        this._lineGraphics._parentClass = this;

        this._fireballGraphics = new PIXI.Graphics();
        this._fireballGraphics._graphicsName = graphicsName;
        this._fireballGraphics._parentClass = this;
    }

    var p = animatedCurve.prototype;

    p.p0 = function(value)
    {
        this._p0 = value;
    }
    p.p1 = function(value)
    {
        this._p1 = value;
    }
    p.p2 = function(value)
    {
        this._p2 = value;
    }

    /**
     * 光效个数
     * @param value
     */
    p.numFireballs = function(value)
    {
        this._numFireballs = value;
    }

    /**
     * 初始化
     */
    p.init = function()
    {
        var length = Math.sqrt(Math.pow(this._p2.x - this._p0.x, 2) + Math.pow(this._p2.y - this._p0.y, 2));
        var midpoint = {x: (this._p0.x + this._p2.x) / 2, y: (this._p0.y + this._p2.y) / 2};
        var k = -1 / ((this._p2.y - this._p0.y) / (this._p2.x - this._p0.x));
        k = Math.atan(k);
        if (k > 0) k -= Math.PI;
        var h = Math.round(length / 3);

        this._p1 = {x: midpoint.x + Math.cos(k) * h, y: midpoint.y + Math.sin(k) * h};

        this._numPieces = 100;
        this._dt = 1 / this._numPieces;
        var a = 1 / this._numPieces * Math.floor(Math.random() * 100);
        if (a > 1) a = 0;
        this._a = a;
        this._t = 1;
        this._s = 0;
    }

    p.appear = function(isAnimated, delay)
    {
        if (isAnimated)
        {
            this._t = 0;
        }
    }

    /**
     * 渲染
     */
    p.render = function()
    {
        if (this._p0 == null || this._p1 == null || this._p2 == null) return;

        var g;
        var xt = 0, yt = 0;

        this._a += 1 / this._numPieces * 2;

        // 渲染连线
        g = this._lineGraphics;
        g.clear();
        g.lineStyle(2, 0xffff43);

        for (var i = this._s; i < this._t; i += this._dt)
        {
            xt = this.calculatePolynomial(i, this._p0.x, this._p1.x, this._p2.x);
            yt = this.calculatePolynomial(i, this._p0.y, this._p1.y, this._p2.y);
            if (i == this._s) g.moveTo(xt, yt);
            else g.lineTo(xt, yt);
        }

        // 渲染光效
        g = this._fireballGraphics;
        g.clear();

        for (var m = 0, s1 = this._a; m < this._numFireballs; m++, s1 += this._dt * 40)
        {
            var s2 = s1 + this._dt * 20;
            for (i = s1; i < s2; i += this._dt)
            {
                var k = i % this._t;
                var n = (i - s1) / (s2 - s1);
                if (isNaN(k) || k < this._s) continue;
                xt = this.calculatePolynomial(k, this._p0.x, this._p1.x, this._p2.x);
                yt = this.calculatePolynomial(k, this._p0.y, this._p1.y, this._p2.y);

                if (n == 0) n = 0.01;
                g.beginFill(0xffff00);
                g.drawCircle(xt, yt, n * 3);
                g.endFill();
            }
        }
    }

    p.calculatePolynomial = function(t, v0, v1, v2)
    {
        var t2 = t * t;
        var mt = 1 - t;
        var mt2 = mt * mt;
        return v0 * mt2 + v1 * 2 * mt * t + v2 * t2;
    }

    AnimatedObject.AnimatedCurve = animatedCurve;
})();

/**
 * 动态化圆环
 */
(function(){
    var animatedRing = function(graphicsName)
    {
        this._graphics1 = new PIXI.Graphics();
        this._graphics1._graphicsName = graphicsName;
        this._graphics1._parentClass = this;

        this._graphics2 = new PIXI.Graphics();
        this._graphics2._graphicsName = graphicsName;
        this._graphics2._parentClass = this;

        this._graphics3 = new PIXI.Graphics();
        this._graphics3._graphicsName = graphicsName;
        this._graphics3._parentClass = this;
    }

    var p = animatedRing.prototype;

    p.p0 = function(value)
    {
        this._p0 = value;
    }

    /**
     * 圆环半径
     * @param value
     */
    p.radius = function(value)
    {
        this._radius = value;
    }

    /**
     * 初始化
     */
    p.init = function()
    {
        this._graphics1.x = this._p0.x;
        this._graphics1.y = this._p0.y;
        this._graphics2.x = this._p0.x;
        this._graphics2.y = this._p0.y;
        this._graphics3.x = this._p0.x;
        this._graphics3.y = this._p0.y;

        this._alpha1 = this._alpha2 = this._alpha3 = 1;
        this._defaultRadius = 0;
        this._radius1 = this._radius2 = this._radius3 = this._defaultRadius;
        this._delay = 0;
        this._stepNum = 50;
        this._stepSize = (this._radius - this._defaultRadius) / this._stepNum;
    }

    /**
     * 渲染
     */
    p.render = function()
    {
        if (this._p0 == null) return;

        this.drawCircle(this._graphics1, this._radius1, this._alpha1);

        this._radius1 += this._stepSize;
        this._alpha1 -= 1 / this._stepNum;
        if (this._radius1 >= this._radius)
        {
        	this._radius1 = this._defaultRadius;
        	this._alpha1 = 1;
        }

        this._delay++;
        var delayNum = this._stepNum * 1 / 3;
        if (this._delay > delayNum)
        {
        	this.drawCircle(this._graphics2, this._radius2, this._alpha2);

	        this._radius2 += this._stepSize;
	        this._alpha2 -= 1 / this._stepNum;
	        if (this._radius2 >= this._radius)
	        {
	        	this._radius2 = this._defaultRadius;
	        	this._alpha2 = 1;
	        }
        }

        if (this._delay > delayNum * 2)
        {
        	this.drawCircle(this._graphics3, this._radius3, this._alpha3);

	        this._radius3 += this._stepSize;
	        this._alpha3 -= 1 / this._stepNum;
	        if (this._radius3 >= this._radius)
	        {
	        	this._radius3 = this._defaultRadius;
	        	this._alpha3 = 1;
	        }
        }
    }

    p.drawCircle = function(g, radius, alpha)
    {
    	g.clear();
    	g.lineStyle(1, 0xffff00);
    	g.drawCircle(0, 0, radius);
    	g.alpha = alpha;
    }

    AnimatedObject.AnimatedRing = animatedRing;
})();

/**
 * 动态化箭头
 */
(function(){
    var animatedArrow = function(graphicsName)
    {
        this._graphics = new PIXI.Graphics();
        this._graphics._graphicsName = graphicsName;
        this._graphics._parentClass = this;
    }

    var p = animatedArrow.prototype;

    p.p0 = function(value)
    {
        this._p0 = value;
    }

    p.radius = function(value)
    {
        this._radius = value;
    }

    p.init = function()
    {
        this._graphics.x = this._p0.x;
        this._graphics.y = this._p0.y;
        this._speed = Math.random() - 0.3;
        this._type = 'up';
        this.draw();
    }

    p.render = function()
    {
        if (this._p0 == null) return;

        this._graphics.y += this._speed;
        if (this._graphics.y > this._p0.y + 8 || this._graphics.y < this._p0.y - 8)
        {
            // 将速度取反
            this._speed *= -1;
        }
    }

    p.draw = function()
    {
        var g = this._graphics;
        g.clear();

        this._heigth = this._radius;
        this._width = this._radius / 1.8;

        if (this._type == 'up')
        {
        	g.lineStyle(1, 0xffff00);
            g.beginFill(0xffff00);
            g.moveTo(0, -this._heigth / 2 + 5).lineTo(-this._width / 2 + 5, this._heigth / 2 - 10).lineTo(-this._width / 2 + 8, this._heigth / 2 - 7).lineTo(this._width / 2 - 8, this._heigth / 2 - 7).lineTo(this._width / 2 - 5, this._heigth / 2 - 10).lineTo(0, -this._heigth / 2 + 5);
            g.moveTo(-this._width / 2 + 5, this._heigth / 2 - 8).lineTo(-this._width / 2 + 5, this._heigth / 2 - 4);
            g.moveTo(this._width / 2 - 5, this._heigth / 2 - 8).lineTo(this._width / 2 - 5, this._heigth / 2 - 4);
            g.endFill();
        }
        else
        {
            g.lineStyle(1, 0xff8d02);
            g.beginFill(0xff8d02);
            g.moveTo(0, this._heigth / 2 - 5).lineTo(-this._width / 2 + 5, -this._heigth / 2 + 5).lineTo(-this._width / 2 + 8, -this._heigth / 2 + 3).lineTo(this._width / 2 - 8, -this._heigth / 2 + 3).lineTo(this._width / 2 - 5, -this._heigth / 2 + 5).lineTo(0, this._heigth / 2 - 5);
            g.moveTo(-this._width / 2 + 3, this._heigth / 2 - 3).lineTo(-this._width / 2 + 6, this._heigth / 2 - 3);
            g.moveTo(this._width / 2 - 3, this._heigth / 2 - 3).lineTo(this._width / 2 - 6, this._heigth / 2 - 3);
            g.endFill();
        }

        g.lineStyle(1, 0xffffff);
        g.moveTo(-this._width / 2 + 3, -this._heigth / 2).lineTo(-this._width / 2, -this._heigth / 2).lineTo(-this._width / 2, -this._heigth / 2 + 3);
        g.moveTo(this._width / 2 - 3, -this._heigth / 2).lineTo(this._width / 2, -this._heigth / 2).lineTo(this._width / 2, -this._heigth / 2 + 3);
        g.moveTo(-this._width / 2, this._heigth / 2 - 3).lineTo(-this._width / 2, this._heigth / 2).lineTo(-this._width / 2 + 3, this._heigth / 2);
        g.moveTo(this._width / 2 - 3, this._heigth / 2).lineTo(this._width / 2, this._heigth / 2).lineTo(this._width / 2, this._heigth / 2 - 3);
    }

    AnimatedObject.AnimatedArrow = animatedArrow;
})();

/**
 * 动态化矩形
 */
(function(){
    var animatedRect = function(graphicsName)
    {
        this._graphics1 = new PIXI.Graphics();
        this._graphics1._graphicsName = graphicsName;
        this._graphics1._parentClass = this;

        this._graphics2 = new PIXI.Graphics();
        this._graphics2._graphicsName = graphicsName;
        this._graphics2._parentClass = this;
    }

    var p = animatedRect.prototype;

    p.p0 = function(value)
    {
        this._p0 = value;
    }

    p.radius = function(value)
    {
        this._radius = value;
    }

    p.init = function()
    {
        this._graphics1.x = this._p0.x;
        this._graphics1.y = this._p0.y;
        this._graphics2.x = this._p0.x;
        this._graphics2.y = this._p0.y;
        this._speed = 0.2 + Math.random();
        this.draw();
    }

    p.render = function()
    {
        if (this._p0 == null) return;

        this._graphics1.y += this._speed;
        this._graphics2.y += this._speed;
        if (this._graphics1.y > this._p0.y + 8 || this._graphics1.y < this._p0.y - 8)
        {
            // 将速度取反
            this._speed *= -1;
        }
    }

    p.draw = function()
    {
        var g = this._graphics1;
        g.clear();

        var edge = this._radius / 1.8;
        g.beginFill(0xffff00);
        // 中间的矩形
        g.drawRect(-edge / 2, -edge / 2, edge, edge);
        // 四个角的矩形
        g.drawRect(-edge / 2 - 2, -edge / 2 - 2, 2, 2)
            .drawRect(edge / 2, -edge / 2 - 2, 2, 2)
            .drawRect(edge / 2, edge / 2, 2, 2)
            .drawRect(-edge / 2 - 2, edge / 2, 2, 2);
        g.endFill();

        var g = this._graphics2;
        g.clear();
        g.alpha = 0.3;
        g.lineStyle(1, 0xffffff).drawCircle(0, 0, this._radius);
    }

    AnimatedObject.AnimatedRect = animatedRect;
})();